package com.example.datastructures;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * Data structure to contain all information and methods for time
 * Utilizes the UNIX time standard
 * 
 * @author Oskar
 */
public class MyTime {

	private long exactTime = 0;

	private int year;
	private int month;
	private int week;
	private int day;
	private int hour;
	private int minute;
	private int second;

	private String totalDate;

	/**
	 * Constructor with string as parameter. Exact time might be off.
	 * String needs to be in format yyyy-MM-dd HH:mm:ss
	 * 
	 * @param String t
	 */
	public MyTime(String t){
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
		try {
			Date date = dateFormat.parse(t);
			
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			week = cal.get(Calendar.WEEK_OF_YEAR);
			
			exactTime = date.getTime();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		totalDate = t;

		year = Integer.parseInt(t.split("-")[0]);
		month = Integer.parseInt(t.split("-")[1]);
		day = Integer.parseInt(t.split("-")[2].split(" ")[0]);

		hour = Integer.parseInt(t.split(" ")[1].split(":")[0]);
		minute = Integer.parseInt(t.split(" ")[1].split(":")[1]);
		second = Integer.parseInt(t.split(" ")[1].split(":")[2]);
	}

	/**
	 * Constructor with a long as parameter. ExactTime will be exact.
	 * The long needs to be taken from System.currentTimeMillis
	 * 
	 * @param long t
	 */
	public MyTime(long t){
		exactTime = t;

		Date date = new Date(t);
		
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		week = (cal.get(Calendar.WEEK_OF_YEAR));
		
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
		String dateString = dateFormat.format(date);

		totalDate = dateString;

		year = Integer.parseInt(dateString.split("-")[0]);
		month = Integer.parseInt(dateString.split("-")[1]);
		day = Integer.parseInt(dateString.split("-")[2].split(" ")[0]);

		hour = Integer.parseInt(dateString.split(" ")[1].split(":")[0]);
		minute = Integer.parseInt(dateString.split(" ")[1].split(":")[1]);
		second = Integer.parseInt(dateString.split(" ")[1].split(":")[2]);
	}

	/**
	 * Calculates the time difference in seconds
	 * between this MyTime object and another MyTime object.
	 * 
	 * @param MyTime mt
	 * @return the result in seconds (as a double)
	 */
	public double getSecondsDifference(MyTime mt){
		double secondsDifference = 0;

		secondsDifference = this.toLong()-mt.toLong();
		secondsDifference = secondsDifference/1000.0;

		return secondsDifference;
	}

	public int getYear() {
		return year;
	}

	public int getMonth() {
		return month;
	}

	public int getDay() {
		return day;
	}

	public int getHour() {
		return hour;
	}

	public int getMinute() {
		return minute;
	}

	public int getSecond() {
		return second;
	}

	@Override
	public String toString() {
		return totalDate;
	}

	public long toLong() {
		return exactTime;
	}

	public int getWeek() {
		return week;
	}
}